package com.landiao.service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.landiao.common.utils.CodecUtils;
import com.landiao.entities.HairdoImg;
import com.landiao.entities.HairdoRecord;
import com.landiao.entities.SuggestVipMdse;
import com.landiao.entities.Vip;
import com.landiao.mapper.HairdoImgMapper;
import com.landiao.mapper.HairdoRecordMapper;
import com.landiao.mapper.SuggestVipMdseMapper;
import com.landiao.mapper.VipMapper;

@Service
public class VipService extends BaseService<Vip, String>{
	
	@Autowired
	private SuggestVipMdseMapper suggestMapper;
	
	@Autowired
	private HairdoRecordMapper hairdoRecordMapper;
	
	@Autowired
	private HairdoImgMapper hairdoImgMapper;

	@Override
	protected VipMapper getBaseMapper() {
		// TODO Auto-generated method stub
		return (VipMapper)baseMapper;
	}
	
	public void updateByPrimaryKeySelective(Vip vip){
		getBaseMapper().updateByPrimaryKeySelective(vip);
	}

	public Vip getByIdWithDict(String vipId) {
		// TODO Auto-generated method stub
		return getBaseMapper().getByIdWithDict(vipId);
	}
	
	public Vip getByVipNo(String vipNo) {
		// TODO Auto-generated method stub
		return getBaseMapper().getByVipNo(vipNo);
	}
	
	public Vip getByOperId(String operId) {
		// TODO Auto-generated method stub
		return getBaseMapper().getByOperId(operId);
	}
	
	/**
	 * 微信端注册
	 */
	public Vip saveFromWx(Vip vip){
		Vip vipByPhone = this.getBaseMapper().getByVipNo(vip.getPhoneNo());
		Vip vipByOperID = this.getBaseMapper().getByOperId(vip.getOperId());
		if(vipByPhone!=null){//已存在
			if(vipByOperID!=null&&!vipByPhone.getVipId().equals(vipByOperID.getOperId())){
				vipByOperID.setOperId(null);
				vipByOperID.setNickname(null);
				this.getBaseMapper().updateByPrimaryKey(vipByOperID);
			}
			vipByPhone.setOperId(vip.getOperId());
			vipByPhone.setNickname(vip.getNickname());
			if(vipByPhone.getSex()==null)
				vipByPhone.setSex(vip.getSex());
			if(StringUtils.isNotEmpty(vip.getName()))
				vipByPhone.setName(vip.getName());
			this.getBaseMapper().updateByPrimaryKey(vipByPhone);
			return vipByPhone;
		}else{
			if(vipByOperID!=null){
				vipByOperID.setOperId(null);
				vipByOperID.setNickname(null);
				this.getBaseMapper().updateByPrimaryKey(vipByOperID);
			}
			vip.setVipId(CodecUtils.getUUID());
			vip.setBalance(new BigDecimal("0"));
			vip.setVipNo(vip.getPhoneNo());
			vip.setPoints(0);
			vip.setStatedate(new Date());
			vip.setIsStar("NO");
			
			this.getBaseMapper().insert(vip);
			
			return vip;
		}
	}
	
	/**
	 * 获取当天生日的会员
	 * @return
	 */
	public List<Vip> getByBirthday(){
		return this.getBaseMapper().getByBirthday();
	}
	
	public List<SuggestVipMdse> getSuggestVipMdseListByVipId(String vipId){
		SuggestVipMdse su = new SuggestVipMdse();
		su.setVipId(vipId);
		return this.suggestMapper.queryByPage(su, null);
	}
	
	public void updateVipInfo(Vip vip,HairdoRecord hairdoRecord,String hairMdse,String scalpMdse){
		Date nowDate = new Date();
		if(vip.getVipId()!=null){
			this.getBaseMapper().updateByPrimaryKeySelective(vip);
		}else{
			vip.setVipId(CodecUtils.getUUID());
			vip.setBalance(new BigDecimal("0"));
			vip.setVipNo(vip.getPhoneNo());
			vip.setPoints(0);
			vip.setStatedate(nowDate);
			vip.setIsStar("NO");
			this.getBaseMapper().insert(vip);
		}
		if(hairdoRecord!=null){
			hairdoRecord.setHairdoRecordId(CodecUtils.getUUID());
			hairdoRecord.setVipId(vip.getVipId());
			hairdoRecord.setStatedate(nowDate);
			this.hairdoRecordMapper.insert(hairdoRecord);
			if(hairdoRecord.getImgList()!=null){
				for(HairdoImg img:hairdoRecord.getImgList()){
					if(StringUtils.isNotEmpty(img.getImg())){
						img.setCompId(vip.getCompId());
						img.setHairdoImgId(CodecUtils.getUUID());
						img.setHairdoRecordId(hairdoRecord.getHairdoRecordId());
						img.setStatedate(nowDate);
						this.hairdoImgMapper.insert(img);
					}
				}
			}
		}
		if(StringUtils.isNotEmpty(hairMdse)){
			this.suggestMapper.deleteByState(vip.getVipId(), "ZDFZJJCP");
			SuggestVipMdse sv = new SuggestVipMdse();
			sv.setCompId(vip.getCompId());
			sv.setSuggestVipMdseId(CodecUtils.getUUID());
			sv.setVipId(vip.getVipId());
			sv.setMdseId(hairMdse);
			sv.setState("ZDFZJJCP");
			sv.setStatedate(nowDate);
			this.suggestMapper.insert(sv);
		}
		if(StringUtils.isNotEmpty(scalpMdse)){
			this.suggestMapper.deleteByState(vip.getVipId(), "TPHLCP");
			SuggestVipMdse sv = new SuggestVipMdse();
			sv.setCompId(vip.getCompId());
			sv.setSuggestVipMdseId(CodecUtils.getUUID());
			sv.setVipId(vip.getVipId());
			sv.setMdseId(scalpMdse);
			sv.setState("TPHLCP");
			sv.setStatedate(nowDate);
			this.suggestMapper.insert(sv);
		}
	}
}
